/**
 * Author:yuiop
 * Date:2022/11/8 17:50
 */
package com.devops.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.devops.admin.service.DOSProjectVersionService;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSVersionOperationVo;
import com.devops.common.utils.Message;
import com.devops.constant.DevOpsConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/project/version")
@Api(value = "/api/project/version",tags = "projectVersion相关接口")
public class ProjectVersionController {
    @Autowired
    DOSProjectVersionService dosProjectVersionService;

    @PostMapping(value = "/close")
    @ApiOperation(value="webhook关闭版本",notes = "调用webhook链接，根据versionId关闭版本")
    @ApiImplicitParams({@ApiImplicitParam(name = "vo",value = "vo",required = true,dataType = "DOSVersionOperationVo",paramType = "body")})
    public Message closeProjectVersion(@RequestBody DOSVersionOperationVo vo) {
        Assert.isTrueMvc(vo.getOperateSourceType() != null && vo.getOperateSourceType() > 0,"操作来源不能为空");
        Assert.isTrueMvc(StrUtil.isNotBlank(vo.getOperateUserName()),"操作人姓名不能为空");
        vo.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.WEBHOOK_PUBLISH_FINISH);
        dosProjectVersionService.processOperation(vo);
        return Message.ok("操作成功");
    }

    @PostMapping(value = "/accept")
    @ApiOperation(value="webhook审核通过",notes = "调用webhook链接，根据versionId审核通过发版申请")
    @ApiImplicitParams({@ApiImplicitParam(name = "vo",value = "vo",required = true,dataType = "DOSVersionOperationVo",paramType = "body")})
    public Message acceptProjectVersion(@RequestBody DOSVersionOperationVo vo) {
        Assert.isTrueMvc(vo.getOperateSourceType() != null && vo.getOperateSourceType() > 0,"操作来源不能为空");
        Assert.isTrueMvc(StrUtil.isNotBlank(vo.getOperateUserName()),"操作人姓名不能为空");
        vo.setOperationType(DevOpsConstant.ProjectVersionOperateActionType.WEBHOOK_REVIEW_ACCEPT);
        dosProjectVersionService.processOperation(vo);
        return Message.ok("操作成功");
    }

    @PostMapping (value = "/callback")
    @ApiOperation(value="钉钉机器人回调地址",notes = "钉钉机器人回调地址")
    public Message callBack(@RequestBody JSONObject jsonObject) {
        log.info("ding talk project version card callback {}", jsonObject);
        DOSVersionOperationVo dosVersionOperationVo =
                JSONUtil.toBean(jsonObject.getJSONObject("content").getJSONObject("cardPrivateData").getJSONObject(
                        "params"), DOSVersionOperationVo.class);
        dosVersionOperationVo.setOperateSourceType(DevOpsConstant.ProjectVersionOperateSourceType.DING_TYPE);
        dosProjectVersionService.processOperation(dosVersionOperationVo);
        return Message.ok("操作成功");
    }
}
